package com.banban.user.service.impl;

import com.banban.user.domain.SysUser;
import com.banban.user.domain.UserProfile;
import com.banban.user.dto.UpdateUserProfileDTO;
import com.banban.user.dto.UserProfileDTO;
import com.banban.user.enums.FieldEnum;
import com.banban.user.exception.BusinessException;
import com.banban.user.mapper.SysUserMapper;
import com.banban.user.mapper.UserProfileMapper;
import com.banban.user.service.UserProfileService;
import com.banban.user.vo.UserProfileDetailVO;
import com.banban.user.vo.UserProfileVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserProfileServiceImpl extends ServiceImpl<UserProfileMapper, UserProfile> implements UserProfileService {

    @Resource
    private UserProfileMapper mapper;

    @Resource
    private SysUserMapper userMapper;

    @Override
    public UserProfileVO getUserProfile(UserProfileDTO dto) {
        String userId = dto.getUserId();
        SysUser sysUser = userMapper.selectById(userId);
        if (sysUser == null || sysUser.getDeleted() == 0){
            throw new BusinessException("用户已被删除或不存在");
        }
        QueryWrapper<UserProfile> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        List<UserProfile> userProfiles = mapper.selectList(qw);
        List<UserProfileDetailVO> base = new ArrayList<>();
        List<UserProfileDetailVO> expand= new ArrayList<>();
        List<UserProfileDetailVO> ai= new ArrayList<>();
        List<UserProfileDetailVO> personalizedDemand= new ArrayList<>();
        if (userProfiles!=null){
            for (UserProfile userProfile : userProfiles) {
                UserProfileDetailVO vo = new UserProfileDetailVO();
                BeanUtils.copyProperties(userProfile,vo);
                vo.setFieldName(FieldEnum.getNameByCode(vo.getField()));
                if ("name".equals(vo.getField()) || "gender".equals(vo.getField())|| "hometown".equals(vo.getField())|| "region".equals(vo.getField())
                        || "maritalStatus".equals(vo.getField())){
                    base.add(vo);
                }else if ("familyStructure".equals(vo.getField()) ||"occupation".equals(vo.getField()) ||"educationLevel".equals(vo.getField())){
                    expand.add(vo);
                }else if ("personalizedDemand".equals(vo.getField())){
                    personalizedDemand.add(vo);
                }else {
                    ai.add(vo);
                }
            }
        }
        UserProfileVO vo = new UserProfileVO();
        vo.setBase(base);
        vo.setAi(ai);
        vo.setExpand(expand);
        vo.setPersonalizedDemand(personalizedDemand);
        return vo;
    }

    @Override
    public List<UserProfile> getList(UserProfileDTO dto) {
        QueryWrapper<UserProfile> qw = new QueryWrapper<>();
        qw.eq("user_id",dto.getUserId());
        return mapper.selectList(qw);

    }

    @Override
    public void updateUserProfile(UpdateUserProfileDTO dto) {
        mapper.updateUserProfile(dto);
    }

    @Override
    public void deleteUserProfile(UpdateUserProfileDTO dto) {
        mapper.deleteById(dto.getId());
    }
}
